% 3个数字的加减乘运算
clear;
% QuestionList
Question     = struct("numLim",[999,999,999],"resultLim",[999,999],"strfunc",[],"resultfunc",[],"Title","");
QuestionList = [];
% 增加一个题目模板
Question.Title      ="a米+b分米+c*10厘米";
Question.numLim     = [9,9,9];
Question.resultLim  = [999,999];
Question.strfunc    = @(a,b,c) sprintf('%2d米+%2d分米+%2d厘米=________厘米',a,b,c*10);
Question.resultfunc = @(a,b,c) [a*100+b*10, a*100+b*10+c*10];
QuestionList = [QuestionList;Question];

Question.Title      ="a米+b*10分米+c*100厘米";
Question.numLim     = [9,9,9];
Question.resultLim  = [99,99];
Question.strfunc    = @(a,b,c) sprintf('%2d米+%2d分米+%2d厘米=________米',a,b*10,c*100);
Question.resultfunc = @(a,b,c) [a+b, a+b+c];
QuestionList = [QuestionList;Question];

Question.Title      ="a米+b分米-c*10厘米";
Question.numLim     = [9,9,9];
Question.resultLim  = [999,999];
Question.strfunc    = @(a,b,c) sprintf('%2d米+%2d分米-%2d厘米=________分米',a,b,c*10);
Question.resultfunc = @(a,b,c) [a*100+b*10, a*100+b*10-c*10];
QuestionList = [QuestionList;Question];

Question.Title      ="a米+b分米+c*10厘米";
Question.numLim     = [9,9,9];
Question.resultLim  = [999,999];
Question.strfunc    = @(a,b,c) sprintf('%2d米+%2d分米+%2d厘米=________分米',a,b,c*10);
Question.resultfunc = @(a,b,c) [a*100+b*10, a*100+b*10+c*10];
QuestionList = [QuestionList;Question];
% 页数
pagenum    = 10;
% 每页题数
numperpage = 20;
% 总题目数
maxcnt     = numperpage*pagenum;
% 问题列表
problemstr = cell(maxcnt, 1);
% 生成题目
for i = 1:maxcnt
    op            = randi([1,length(QuestionList)]);
    problemstr{i} = GenOneQuestion(QuestionList(op), false);
end
% 生成文档
GenDocument(problemstr, "mydoc", pagenum);

function str = GenOneQuestion(Question, withAnswer)
    while true
        num1 = randi(Question.numLim(1));
        num2 = randi(Question.numLim(2));
        num3 = randi(Question.numLim(3));
        result = Question.resultfunc(num1, num2, num3);
        if (result(1) < Question.resultLim(1) && result(1) >= 0) && ...
                (result(2) < Question.resultLim(2) && result(2) >= 0)
            str = Question.strfunc(num1, num2, num3);
            if withAnswer   
                str = [str, num2str(result(2))];%#ok
            end
            return;
        end
    end
end
function GenDocument(problemstr, filename, pagenum)
% 参数
% problemstr 题目列表
% filename 文件名
% pagenum 页数
import mlreportgen.dom.*
d = Document(filename,"docx");
open(d);
cnt   = 1;
for i = 1:pagenum
    % 表格式
    tableStyles       = { ColSep("solid"), RowSep("solid"), Border("solid"), Width("100%"), RowHeight('60pt','atleast'), FontSize("12pt")};
    % 表头格式
    tableHeaderStyles = { BackgroundColor("lightgray"), Bold(true), FontSize("16pt"),RowHeight('20pt')};
    % 表头名称
    headerLabels      = ["姓名：___开始： ____","结束： ____得分： ____"];
    % 表数据 5行4列
    tableData = reshape(problemstr( cnt:(cnt+19),1),10,2);
    cnt       = cnt + 20;
    cellTbl   = FormalTable(headerLabels,tableData);
    cellTbl.Style = [cellTbl.Style, tableStyles];
    cellTbl.Header.Style = [cellTbl.Header.Style, tableHeaderStyles];
    cellTbl.TableEntriesInnerMargin = "2pt";
    append(d,cellTbl);
    if i ~= pagenum
        br = PageBreak();
        append(d,br);
    end
end
close(d);
end